import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import java.util.Stack;

/**
 * @author Huhao
 * &#064;date  2024/12/11
 */

class LR0 extends JFrame implements ActionListener{

    JFrame frame;

    JButton b0;
    JPanel p1,p2,p3,p4,p5,p6;
    static JTextArea t1,t2,t3;
    JButton b1,b2,b3,b4;
    JLabel l0,l1,l3;
    Box vbox;

    public LR0()
    {
        frame = new JFrame();
        frame.setTitle("LR（0）分析");
        frame.setLocation(500,200);
        frame.setSize(800,600);

        b0=new JButton("读入文法(S为开始)");
        b3=new JButton("清空");
        b4 =new JButton("形式转换");
        b1=new JButton("开始分析");
        l0=new JLabel("输入字符串：");
        t2=new JTextArea(1,30);
        b2=new JButton("判断");
        l1=new JLabel("输入的文法为：");
        l3=new JLabel("                                                                         分析表如下：                                    ");

        vbox = Box.createVerticalBox();

        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        p4=new JPanel();
        p5=new JPanel();
        p6=new JPanel();

        t1=new JTextArea(24,20);
        t3=new JTextArea(24,40);


        JScrollPane jp1=new JScrollPane(t1);
        JScrollPane jp2=new JScrollPane(t2);
        JScrollPane jp3=new JScrollPane(t3);

        vbox.add(p4);
        vbox.add(p5);
        vbox.add(p6);
        p4.add(b0);
        p4.add(b4);
        p4.add(b3);
        p4.add(b1);


        p5.add(l0);
        p5.add(jp2);
        p5.add(b2);


        p6.add(l1);
        p6.add(l3);
        p1.add(vbox);
        p2.add(jp1);
        p2.add(jp3);

        frame.add(p1,"North");
        frame.add(p2,"Center");

        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }



    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b0)//读文件
        {
            File getPath;
            JFileChooser jf=new JFileChooser("./");
            FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文档(*.txt)", "txt");
            jf.setFileFilter(filter);
            int value=jf.showOpenDialog(frame);
            if(value==JFileChooser.APPROVE_OPTION){
                getPath=jf.getSelectedFile();
                File fp=getPath;
                BufferedReader reader = null;
                String tempString;
                String sum="";
                try {
                    reader = new BufferedReader(new FileReader(fp));
                    while ((tempString = reader.readLine()) != null) {
                        if(sum.equals(""))
                            sum=sum+tempString;
                        else
                            sum=sum+"\n"+tempString;
                    }
                    this.t1.setText(sum);
                    reader.close();
                } catch (FileNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } finally{
                    if(reader != null){
                        try {
                            reader.close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        }
        if(e.getSource()== b4)//形式转换
        {
            String s_ori[]=t1.getText().split("\n");
            String s[]=new String[100];
            t1.setText("");

            int temp=0;
            int flag=0;
            for(int i=0;i< s_ori.length;i++){

                String left = s_ori[i].split("->")[0];//左部
                if(left.equals("S")){
                    flag=1;
                }

                String right = s_ori[i].split("->")[1];//右部
                String rights[] = right.split("\\|");
                for(int j=0;j<rights.length;j++){
                    s[temp]=left+"->"+rights[j];
                    temp++;

                }
            }
            char start = s[0].charAt(0);
            for(int i=0;s[i]!=null;i++){
                if(i==0){
                    t1.append(s[i]);
                }
                else{
                    t1.append("\n");
                    t1.append(s[i]);
                }
            }
        }

        if(e.getSource()==b1)
        {
            String s[]=t1.getText().split("\n");
            LRAnalyser lrAnalyser = new LRAnalyser(s);
        }
        if(e.getSource()==b3)
        {
            t1.setText("");
            t2.setText("");
            t3.setText("");
        }
        if(e.getSource()==b2)
        {
            LexicalAnalyser lexicalAnalyser = new LexicalAnalyser("LR0");
            java.util.List<Token> tokenlist = lexicalAnalyser.analise(LRAnalyser.G);
            Stack<Token> stack = lexicalAnalyser.convertToStack(LRAnalyser.G, tokenlist);
            LRAnalyser.analyse(stack, LRAnalyser.G, LRAnalyser.analyserTable);
        }
    }

//    public static void main(String args[]){
//        new LR0();
//    }
}
